<?php
/**
 * 教师列表管理
 * @author a
 *
 */
class Teacher extends CT_Controller{
        
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
                
        $this->load->model('teacher_model');
        $this->load->model('teacher_work_time_model');
                
    }
    /**
     * 修改时间
     */
    public function Updatetime(){
        try {
        	/*此处替换教师ID*/
        	if(empty($this->_is_teacher))throw new Exception('您不是教师');
        	$id = $this->_uid;
        	if(empty($id))throw new Exception('ID为空');
        	
        	//获取时间信息
        	$teacher_work_time_list = $this->teacher_work_time_model->findAll(array('teacher_id'=>$id));
        	if(!empty($teacher_work_time_list)){
        		$tmp = array();
        		foreach ($teacher_work_time_list as $key=>$val){
        			$tmp[$val['work_week']][strtotime($val['work_time_start']).'-'.strtotime($val['work_time_end'])] = $val;
        		}
        		$teacher_work_time_list = $tmp;
        	}
        	$this->ct_template->write('_title', '时间');
        	$this->ct_template->write_view('content', 'teacher/updatetime',array(
        			'teacher_work_time_list'	=>	$teacher_work_time_list,
        			'id'	=>	$id,
        			'type_name'	=>	array('周日','周一','周二','周三','周四','周五','周六'),
        	), FALSE);
        	$this->ct_template->render();
        }catch (Exception $e){
            $this->ct_template->write('_title', '错误提示');
            $this->ct_template->write_view('content', 'errors/html/warn', array('msg'=>$e->getMessage() ), FALSE);
            $this->ct_template->render();
        }
    }
    /**
     * 修改时间ajax
     */
    public function Updatetimeajax(){
    	try {
    		$return = array('code'=>10,'msg'=>'OK','id'=>0);
    		$checked = $this->input->get('checked');
    		$teacher_id = intval($this->input->get('teacher_id'));
    		if(empty($teacher_id))throw new Exception('教师ID为空');
    		$id = intval($this->input->get('id'));	//	存在则修改
    		$work_week = intval($this->input->get('work_week'));
    		$time_data = $this->input->get('time_data');
    		if(empty($time_data))throw new Exception('时间为空');
    		$time_data = explode('-',$time_data);
    		$data = array();
    		$data['teacher_id'] = $teacher_id;
    		$data['work_week'] = $work_week;
    		$data['work_time_start'] = $time_data[0];
    		$data['work_time_end'] = $time_data[1];
    		$data['status'] = $checked == 'true' ? 1 : 0;
    		if(!empty($id)){	//修改
    			$data['update_date'] = date('Y-m-d H:i:s');
    			$bool = $this->teacher_work_time_model->update($data,array('id'=>$id));
    			if(empty($bool))throw new Exception('失败');
    			$return['id'] = $id;
    		}else{		//添加
    			$data['create_date'] = date('Y-m-d H:i:s');
    			$bool = $this->teacher_work_time_model->insert($data);
    			if(empty($bool))throw new Exception('失败');
    			$return['id'] = $this->teacher_work_time_model->getInsertID();
    		}
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    	exit;
    }
}






























